//
//  YGOwnHelpViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/11.
//

#import "YGOwnHelpViewController.h"
#import "YGDropdownListView.h"
#import "YGOwnHelpBLL.h"
#import "YGSickListViewController.h"
#import "YGPrescriptionViewController.h"
@interface YGOwnHelpViewController ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UILabel *genderLabel;
@property (nonatomic, strong) UILabel *heightLabel;
@property (nonatomic, strong) UILabel *heightUnitLabel;        //单位cm

@property (nonatomic, strong) UILabel *weightLabel;
@property (nonatomic, strong) UILabel *weightUnitLabel;        //单位kg

@property (nonatomic, strong) UILabel *labourStrengthLabel;

@property (nonatomic, strong) YGDropdownListView *genderListView;   //下拉列表
@property (nonatomic, strong) YGDropdownListView *labourStrengthListView;    //下拉列表
@property (nonatomic, assign) bool showed;

@property (nonatomic, strong)UIButton *genderBtn;
@property (nonatomic, strong)UIButton *labourStrengthBtn;


@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *ageTextField;
@property (nonatomic, strong) UITextField *heightTextField;
@property (nonatomic, strong) UITextField *weightTextField;

@property (nonatomic, strong) UIButton *hospitalDiagnosisBtn;
@property (nonatomic, strong) UILabel *hospitalDiagnosisLabel;
@property (nonatomic, strong) UIButton *historyBtn;
@property (nonatomic, strong) UILabel *diagnosisLabel;      //显示诊断结果
@property (nonatomic, strong) UIImageView *infoImageView;   //小箭头

@property (nonatomic, assign) NSInteger  sex;
@property (nonatomic, strong) NSArray  *ldArray;
@property (nonatomic, strong) NSString  *ldSelectedStr;


@property (nonatomic, strong) UIButton *submitBtn;

@property (nonatomic, strong) NSString  *pid;



@end

@implementation YGOwnHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"营养自助方案"];
    //设置导航栏属性
    //self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x333333)}];
    
    [self initView];
    [self initconstraints];
    [self requestData];
    
    self.nameTextField.text = @"ll";
    self.ageTextField.text = @"18";
    self.weightTextField.text = @"50";
    self.heightTextField.text = @"180";
}

-(void)initView{
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.ageLabel];
    [self.view addSubview:self.genderLabel];
    [self.view addSubview:self.heightLabel];
    [self.view addSubview:self.weightLabel];
    [self.view addSubview:self.labourStrengthLabel];
    [self.view addSubview:self.heightUnitLabel];
    [self.view addSubview:self.weightUnitLabel];
    
    [self.view addSubview:self.nameTextField];
    [self.view addSubview:self.ageTextField];
    [self.view addSubview:self.heightTextField];
    [self.view addSubview:self.weightTextField];
    [self.view addSubview:self.genderListView];
    [self.view addSubview:self.labourStrengthListView];
    [self.view addSubview:self.genderBtn];
    [self.view addSubview:self.labourStrengthBtn];
    
    [self.view addSubview:self.hospitalDiagnosisBtn];
    [self.hospitalDiagnosisBtn addSubview:self.hospitalDiagnosisLabel];
    [self.hospitalDiagnosisBtn addSubview:self.historyBtn];
    [self.hospitalDiagnosisBtn addSubview:self.diagnosisLabel];
    [self.hospitalDiagnosisBtn addSubview:self.infoImageView];
    
    [self.view addSubview:self.submitBtn];
}

-(void)initconstraints{
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top).offset(110);
            make.left.mas_equalTo(self.view.mas_left).offset(10);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
            
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.width.mas_equalTo(100);
            make.left.mas_equalTo(self.nameLabel.mas_right).offset(40);
    }];
    
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(40);
            make.left.mas_equalTo(self.nameLabel.mas_left);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
    }];
    
    [self.ageTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.ageLabel.mas_centerY);
            make.left.mas_equalTo(self.ageLabel.mas_right).offset(40);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.ageLabel.mas_bottom).offset(40);
            make.left.mas_equalTo(self.ageLabel.mas_left);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
    }];
    
    [self.genderListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.genderLabel.mas_centerY);
        make.left.mas_equalTo(self.ageTextField.mas_left);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(12);
        
    }];
    [self.heightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.genderLabel.mas_bottom).offset(40);
            make.left.mas_equalTo(self.genderLabel.mas_left);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
    }];
    
    [self.heightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.heightLabel.mas_centerY);
            make.left.mas_equalTo(self.heightLabel.mas_right).offset(40);
    }];
    
    [self.heightUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.heightTextField.mas_centerY);
            make.right.mas_equalTo(self.view.mas_right).offset(-10);
    }];
    
    [self.weightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.heightLabel.mas_bottom).offset(40);
            make.left.mas_equalTo(self.heightLabel.mas_left);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
    }];
    
    [self.weightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.weightLabel.mas_centerY);
            make.left.mas_equalTo(self.weightLabel.mas_right).offset(40);
    }];
    
    [self.weightUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.weightTextField);
            make.right.mas_equalTo(self.view.mas_right).offset(-10);
    }];
    
    [self.labourStrengthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.weightLabel.mas_bottom).offset(40);
            make.left.mas_equalTo(self.weightLabel.mas_left);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(12);
    }];
    
    [self.labourStrengthListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.weightLabel.mas_bottom).offset(40);
        make.left.mas_equalTo(self.weightTextField.mas_left);
        make.height.mas_equalTo(26);
        make.width.mas_equalTo(120);

    }];
    
    [self.hospitalDiagnosisBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.labourStrengthLabel.mas_bottom).offset(22);
            make.left.right.mas_equalTo(self.view);
            make.height.mas_equalTo(50);
    }];
    
    [self.hospitalDiagnosisLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.hospitalDiagnosisBtn.mas_top).offset(20);
            make.left.mas_equalTo(self.hospitalDiagnosisBtn.mas_left).offset(10);
            make.height.mas_equalTo(12);
            make.width.mas_equalTo(60);
    }];
    
    [self.diagnosisLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.hospitalDiagnosisLabel.mas_top);
            make.right.mas_equalTo(self.hospitalDiagnosisBtn.mas_right).offset(-30);
            make.height.mas_equalTo(12);
            make.width.mas_equalTo(60);
    }];
    

    
    [self.infoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.diagnosisLabel.mas_centerY);
            make.right.mas_equalTo(self.hospitalDiagnosisBtn.mas_right).offset(-10);
            make.width.mas_equalTo(14);
            make.height.mas_equalTo(14);
    }];
    
    [self.historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.infoImageView.mas_bottom).offset(20);
            make.right.mas_equalTo(self.infoImageView.mas_right);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(12);
    }];
    
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-30);
            make.left.mas_equalTo(self.view.mas_left).offset(10);
            make.right.mas_equalTo(self.view.mas_right).offset(-10);
            make.height.mas_equalTo(40);
    }];
    

}

- (void)requestData{
    WeakSelf(self);
    [YGOwnHelpBLL requestLaodongSuccess:^(NSArray<YGSickTypeModel *> * _Nonnull model) {
        NSMutableArray *array = [NSMutableArray array];
        for (YGSickTypeModel *sickModel in model) {
            [array addObject:sickModel.name];
        }
        weakself.ldArray = model;
        weakself.labourStrengthListView.tableArray = array ;
    } fail:^(NSError * _Nonnull error) {
        
    }];
}
#pragma event

-(void)clickSubmitAction{
    OwnHelpRequestModel *model = [[OwnHelpRequestModel alloc] init];
    model.name = self.nameTextField.text;
    model.age = [self.ageTextField.text intValue];
    model.sex = self.sex;
    model.stature = [self.heightTextField.text intValue];
    model.weight = [self.weightTextField.text intValue];
    model.labourIntension = [self.ldSelectedStr intValue];
    model.diseaseId = self.pid;
    WeakSelf(self);
    [YGOwnHelpBLL requestServerDetailWithModel:model Success:^(NSString *pid){
        YGPrescriptionViewController *vc = [[YGPrescriptionViewController alloc] init];
        vc.pid = pid;
        [weakself.navigationController pushViewController:vc animated:YES];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(void)clickInfoDiagnosisAction{
    YGSickListViewController *vc = [[YGSickListViewController alloc] init];
    WeakSelf(self);
    vc.ClickBlock = ^(YGFoodGuideTypeModel * _Nonnull pid) {
        weakself.pid = pid.id;
        weakself.diagnosisLabel.text = pid.name;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickLookHistoryAction{
    
}

#pragma setter

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.text = @"姓名：";
        _nameLabel.font = [UIFont systemFontOfSize:12];
        _nameLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.nameLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.nameLabel.mas_left);
            make.height.mas_equalTo(0.5);
            make.width.mas_equalTo(370);
        }];
    }
    return _nameLabel;
}

- (UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc]init];
        _nameTextField.placeholder =@"请输入姓名";
        _nameTextField.font = [UIFont systemFontOfSize:12];
        _nameTextField.layer.borderWidth = 0;
        _nameTextField.textAlignment = NSTextAlignmentCenter;
        
    }
    return _nameTextField;
}

- (UILabel *)ageLabel{
    if (!_ageLabel) {
        _ageLabel = [[UILabel alloc]init];
        _ageLabel.text = @"年龄：";
        _ageLabel.font = [UIFont systemFontOfSize:12];
        _ageLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.ageLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.ageLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.ageLabel.mas_left);
            make.height.mas_equalTo(0.5);
            make.width.mas_equalTo(370);
        }];
    }
    return _ageLabel;
}

- (UITextField *)ageTextField{
    if (!_ageTextField) {
        _ageTextField = [[UITextField alloc]init];
        _ageTextField.placeholder =@"请输入年龄";
        _ageTextField.font = [UIFont systemFontOfSize:12];
        _ageTextField.layer.borderWidth = 0;
        _ageTextField.textAlignment = NSTextAlignmentCenter;
        
    }
    return _ageTextField;
}

- (UILabel *)genderLabel{
    if (!_genderLabel) {
        _genderLabel = [[UILabel alloc]init];
        _genderLabel.text = @"性别：";
        _genderLabel.font = [UIFont systemFontOfSize:12];
        _genderLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.genderLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.genderLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.genderLabel.mas_left);
            make.height.mas_equalTo(0.5);
            make.width.mas_equalTo(370);
        }];
    }
    return _genderLabel;
}

- (UILabel *)heightLabel{
    if (!_heightLabel) {
        _heightLabel = [[UILabel alloc]init];
        _heightLabel.text = @"身高：";
        _heightLabel.font = [UIFont systemFontOfSize:12];
        _heightLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.heightLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.heightLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.heightLabel.mas_left);
            make.height.mas_equalTo(0.5);
            make.width.mas_equalTo(370);
        }];
        
    }
    return _heightLabel;
}

- (UITextField *)heightTextField{
    if (!_heightTextField) {
        _heightTextField = [[UITextField alloc]init];
        _heightTextField.placeholder =@"请输入身高";
        _heightTextField.font = [UIFont systemFontOfSize:12];
        _heightTextField.layer.borderWidth = 0;
        _heightTextField.textAlignment = NSTextAlignmentCenter;
        
    }
    return _heightTextField;
}

- (UILabel *)heightUnitLabel{
    if (!_heightUnitLabel) {
        _heightUnitLabel = [[UILabel alloc]init];
        _heightUnitLabel.text =@"cm";
        _heightUnitLabel.font = [UIFont systemFontOfSize:12];
        _heightUnitLabel.textColor = UIColorFromRGB(0x999999);
    }
    return _heightUnitLabel;
}

- (UILabel *)weightLabel{
    if (!_weightLabel) {
        _weightLabel = [[UILabel alloc]init];
        _weightLabel.text = @"体重：";
        _weightLabel.font = [UIFont systemFontOfSize:12];
        _weightLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.weightLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.weightLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.weightLabel.mas_left);
            make.height.mas_equalTo(0.5);
            make.width.mas_equalTo(370);
        }];
    }
    return _weightLabel;
}

- (UITextField *)weightTextField{
    if (!_weightTextField) {
        _weightTextField = [[UITextField alloc]init];
        _weightTextField.placeholder =@"请输入体重";
        _weightTextField.font = [UIFont systemFontOfSize:12];
        _weightTextField.layer.borderWidth = 0;
        _weightTextField.textAlignment = NSTextAlignmentCenter;
        
    }
    return _weightTextField;
}

- (UILabel *)weightUnitLabel{
    if (!_weightUnitLabel) {
        _weightUnitLabel = [[UILabel alloc]init];
        _weightUnitLabel.text =@"kg";
        _weightUnitLabel.font = [UIFont systemFontOfSize:12];
        _weightUnitLabel.textColor = UIColorFromRGB(0x999999);
    }
    return _weightUnitLabel;
}

- (UILabel *)labourStrengthLabel{
    if (!_labourStrengthLabel) {
        _labourStrengthLabel = [[UILabel alloc]init];
        _labourStrengthLabel.text = @"劳动强度";
        _labourStrengthLabel.font = [UIFont systemFontOfSize:12];
        _labourStrengthLabel.textColor = UIColorFromRGB(0x333333);
        
        UIView *underline=[[UIView alloc] init];
        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.labourStrengthLabel addSubview:underline];
        
        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.labourStrengthLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.labourStrengthLabel.mas_left);
            make.height.mas_equalTo(2);
            make.width.mas_equalTo(370);
        }];
    }
    return _labourStrengthLabel;
    

}

- (UIButton *)hospitalDiagnosisBtn{
    if (!_hospitalDiagnosisBtn) {
        _hospitalDiagnosisBtn = [[UIButton alloc]init];
        [_hospitalDiagnosisBtn setBackgroundColor:[UIColor whiteColor]];
        [_hospitalDiagnosisBtn addTarget:self action:@selector(clickInfoDiagnosisAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _hospitalDiagnosisBtn;
}

- (UILabel *)hospitalDiagnosisLabel{
    if (!_hospitalDiagnosisLabel) {
        _hospitalDiagnosisLabel = [[UILabel alloc]init];
        _hospitalDiagnosisLabel.text = @"医院诊断";
        _hospitalDiagnosisLabel.font = [UIFont systemFontOfSize:12];
        _hospitalDiagnosisLabel.textColor = UIColorFromRGB(0x333333);
        
    }
    return _hospitalDiagnosisLabel;
}

- (UILabel *)diagnosisLabel{
    if (!_diagnosisLabel) {
        _diagnosisLabel = [[UILabel alloc]init];
        _diagnosisLabel.font = [UIFont systemFontOfSize:12];
        _diagnosisLabel.textColor = UIColorFromRGB(0x333333);
    }
    return _diagnosisLabel;
}

- (UIImageView *)infoImageView{
    if (!_infoImageView) {
        _infoImageView = [[UIImageView alloc]init];
        [_infoImageView setImage:[UIImage imageNamed:@"ownhelp_info_icon"]];
    }
    return _infoImageView;
}

- (UIButton *)historyBtn{
    if (!_historyBtn) {
        _historyBtn = [[UIButton alloc]init];
        [_historyBtn addTarget:self action:@selector(clickLookHistoryAction) forControlEvents:UIControlEventTouchUpInside];
        [_historyBtn setTitle:@"查看历史记录" forState:UIControlStateNormal];
        [_historyBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateNormal];
        _historyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    }
    return _historyBtn;
}


- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [[UIButton alloc]init];
        [_submitBtn setBackgroundColor:UIColorFromRGB(0x37c8d7)];
        [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _submitBtn.layer.cornerRadius = 5;
        _submitBtn.clipsToBounds = YES;
        [_submitBtn addTarget:self action:@selector(clickSubmitAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

- (YGDropdownListView *)labourStrengthListView{
    if (!_labourStrengthListView) {
        _labourStrengthListView = [[YGDropdownListView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        WeakSelf(self);
        [_labourStrengthListView setCallBack:^(BOOL show) {
            weakself.showed = show;
        }];
        [_labourStrengthListView setSelectCallBack:^(NSInteger row) {
            NSLog(@"----ar %d",weakself.ldArray.count);
            YGSickTypeModel *model = [weakself.ldArray objectAtIndex:row];
            weakself.ldSelectedStr = model.value;
        }];
        _labourStrengthListView.tv.backgroundColor = [UIColor whiteColor];
    }
    return _labourStrengthListView;
}

- (YGDropdownListView *)genderListView{
    if (!_genderListView) {
        _genderListView = [[YGDropdownListView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        WeakSelf(self);
        [_genderListView setCallBack:^(BOOL show) {
            weakself.showed = show;
        }];
        [_genderListView setSelectCallBack:^(NSInteger row) {
            if (row == 0) {
                weakself.sex = 1;
            }else{
                weakself.sex = 2;
            }
        }];
        _genderListView.tableArray = @[@"男",@"女"];
        _genderListView.tv.backgroundColor = [UIColor whiteColor];
    }
    return _genderListView;
}

- (NSInteger)sex{
    if (_sex == 0) {
        _sex = 1;
    }
    return _sex;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
